package com.ft.admix.gdt.adapter;

import android.app.Activity;
import android.content.Context;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;
import com.ft.admix.ad.MixCustomInterstitialAdapter;
import com.ft.admix.config.AdConfig;

public class InterstitialAdLoader extends MixCustomInterstitialAdapter {
    private TTFullScreenVideoAd mTTFullScreenVideoAd;

    @Override
    protected void loadNetworkAd(Activity context, AdConfig config) {
        if (!TTAdSdk.isSdkReady()) {

        }
        AdSlot adslot = new AdSlot.Builder()
                .setCodeId(config.getAdId())
                .setOrientation(TTAdConstant.ORIENTATION_VERTICAL)//设置横竖屏方向
                .setMediationAdSlot(new MediationAdSlot.Builder()
                        .setMuted(true)//是否静音
                        .setVolume(0.7f)//设置音量
                        .setBidNotify(true)//竞价结果通知
                        .build())
                .build();
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(context);
        adNativeLoader.loadFullScreenVideoAd(adslot, new TTAdNative.FullScreenVideoAdListener() {


            @Override
            public void onError(int i, String s) {
                callLoadFailed(i, s);
            }

            @Override
            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ttFullScreenVideoAd) {
                mTTFullScreenVideoAd = ttFullScreenVideoAd;
                callLoadSuccess();
                mTTFullScreenVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {
                    @Override
                    public void onAdShow() {
                        callInterstitialAdShow();
                    }

                    @Override
                    public void onAdVideoBarClick() {
                        callInterstitialAdClick();
                    }

                    @Override
                    public void onAdClose() {
                        callInterstitialAdClose();
                    }

                    @Override
                    public void onVideoComplete() {
                        callInterstitialVideoComplete();
                    }

                    @Override
                    public void onSkippedVideo() {
                        callInterstitialAdSkip();
                    }
                });
            }

            @Override
            public void onFullScreenVideoCached() {

            }

            @Override
            public void onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd) {

            }
        });
    }

    @Override
    public void show(Activity activity) {
        if (this.mTTFullScreenVideoAd != null) {
            this.mTTFullScreenVideoAd.showFullScreenVideoAd(activity);
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        if (mTTFullScreenVideoAd != null && mTTFullScreenVideoAd.getMediationManager() != null) {
            mTTFullScreenVideoAd.getMediationManager().destroy();
        }
    }
}
